package com.xmh.security_1.entity;

import java.util.*;
import java.math.BigDecimal;

import com.alibaba.fastjson.JSONObject;

import java.sql.Timestamp;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;

@Data
public class UserEntity implements UserDetails {

    /**
     * 版本号
     * 表备注：
     */
    private static final long serialVersionUID = 2847884406964673852L;

    /**
     *
     */
    private String password;

    /**
     *
     */
    private Integer id;

    /**
     *
     */
    private String username;

    /**
     *
     */
    private String nickname;

    /**
     *
     */
    private Boolean userEnabled;

    /**
     *
     */
    private Date createDate;

    /**
     *
     */
    private Date updateDate;


    private List<RolesEntity> rolesEntityList;

    public UserEntity(String json) {
        this(JSONObject.parseObject(json, Map.class));
    }

    public UserEntity() {
    }

    public UserEntity(Map<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return;
        }

        password = (String) map.get("password");

        if (map.get("id") != null) {
            id = Integer.valueOf(String.valueOf(map.get("id")));
        }

        username = (String) map.get("username");

        nickname = (String) map.get("nickname");

        if (map.get("userEnabled") != null) {
            userEnabled = Boolean.valueOf(String.valueOf(map.get("userEnabled")));
        }

        Object createDateobj = map.get("createDate");
        if (createDateobj != null) {
            if (createDateobj instanceof Date) {
                createDate = (Date) createDateobj;
            } else {
                createDate = new Date((long) createDateobj);
            }
        }

        Object updateDateobj = map.get("updateDate");
        if (updateDateobj != null) {
            if (updateDateobj instanceof Date) {
                updateDate = (Date) updateDateobj;
            } else {
                updateDate = new Date((long) updateDateobj);
            }
        }

    }

    public Map toMap() {
        Map map = new HashMap();
        map.put("password", password);
        map.put("id", id);
        map.put("username", username);
        map.put("nickname", nickname);
        map.put("userEnabled", userEnabled);
        map.put("createDate", createDate);
        map.put("updateDate", updateDate);
        return map;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return userEnabled;
    }

    @Override
    public List<GrantedAuthority> getAuthorities() {
        if (rolesEntityList == null) {
            // test
            return null;
        }

        List<GrantedAuthority> authorities = new ArrayList<>();
        for (RolesEntity role : rolesEntityList) {
            authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName()));
        }
        return authorities;
    }

}